﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
//using ThreadLibrary;

namespace MagicCardManager
{
    public class MCMController
    {

        private SqLiteHandle.UpdateDataeventhandler sqliteDelegate;

        #region CONST
        private const int maxAnzThreads = 10;
        #endregion CONST

        #region VARIABLES
        private MCMView m_view;
        private SqLiteHandle m_sqLite = null;
        /// <summary>
        /// Hauptdataset
        /// </summary>
        private dsDb data;
        

        /// <summary>
        /// Benachrichtigt einen oder mehrere wartende Threads über das Eintreten eines Ereignisses. Diese Klasse kann nicht vererbt werden.
        /// </summary>
        private ManualResetEvent[] doneEvents = null;
        #endregion VARIABLES

        #region DELEGATE
        public delegate void UpdateMainDataGrideventhandler(object sender, MainDataGridEventArgs e);
        public event UpdateMainDataGrideventhandler UpdateMainData;
        #endregion DELEGATE


        public dsDb Data
        {
            get
            {
                return (data);
            }
            set
            {
                data = value;
            }
        }

        #region Constructor

        public MCMController (MCMView view)
        {
            m_view = view;
            m_sqLite = null;
            
            m_sqLite = new SqLiteHandle(m_view.Config.Get("database"));
            
            //m_sqLite.UpdateData += new SqLiteHandle.UpdateDataeventhandler(OnDataChange);

            doneEvents = new ManualResetEvent[maxAnzThreads];

            //ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i);
           // ThreadPool.QueueUserWorkItem(m_model.ThreadPoolCallback_FillCardGrid);
            
            
            //... Add listeners to the view.
            //view.addMultiplyListener(new MultiplyListener());
            //view.addClearListener(new ClearListener());
        }
        #endregion Constructor


        #region EVENTS
        private void OnDataChange(object source, SQLiteDataEventArgs e)
        {
            //m_view.SDDB = (dsDb)e.DBDataSet;
            OnUpdateMainDataGrid();
            //UpdateMainData(this, new MainDataGridEventArgs(dbDataSet));
            //System.Windows.Forms.Control[] ctrl = m_view.Controls.Find("dataGridView_Main", true);
            //.TextChanged += new System.EventHandler(this.textBox_Search_Name_TextChanged);
            //ctrl[0].TextChanged += new System.EventHandler(textBox_Search_Name_TextChanged);

            //ctrl[0].Update();
        }

        protected void OnUpdateMainDataGrid()
        {
            UpdateMainData(this, new MainDataGridEventArgs(data));
        }


        public dsDb FillCardGrid()
        {
            m_sqLite = null;
            m_sqLite = new SqLiteHandle(m_view.Config.Get("database"));
            //m_model.UpdateData += new SqLiteHandle.UpdateDataeventhandler(OnDataChange);
            dsDb db = m_sqLite.FillCardGrid();

            return (db);
            //ThreadPool.QueueUserWorkItem(m_sqLite.ThreadPoolCallback_FillCardGrid);
            //ThreadPool.QueueUserWorkItem(m_model.ThreadPoolCallback_FillCardGrid);

            //this.textBox_Search_Name.Text
        }

        public dsDb UpdateCardGrid(DataRow dc)
        {
            m_sqLite = null;
            m_sqLite = new SqLiteHandle(m_view.Config.Get("database"));
            dsDb db = m_sqLite.UpdateCardGrid(dc);

            return (db);
        }

        

        public dsDb Search_tbl_CardGrid(string k, string v)
        {
            m_sqLite = null;
            m_sqLite = new SqLiteHandle(m_view.Config.Get("database"));
            //m_model.UpdateData += new SqLiteHandle.UpdateDataeventhandler(OnDataChange);
            dsDb db = m_sqLite.Search_tbl_CardGrid(k, v);

            return (db);
            //ThreadPool.QueueUserWorkItem(m_sqLite.ThreadPoolCallback_FillCardGrid);
            //ThreadPool.QueueUserWorkItem(m_model.ThreadPoolCallback_FillCardGrid);

            //this.textBox_Search_Name.Text
        }

        private void textBox_Search_Name_TextChanged(object sender, EventArgs e)
        {
            m_sqLite = null;
            m_sqLite = new SqLiteHandle(m_view.Config.Get("database"));
            m_sqLite.UpdateData += new SqLiteHandle.UpdateDataeventhandler(OnDataChange);
            ThreadPool.QueueUserWorkItem(m_sqLite.ThreadPoolCallback_FillCardGrid);
            //ThreadPool.QueueUserWorkItem(m_model.ThreadPoolCallback_FillCardGrid);

            //this.textBox_Search_Name.Text
        }

        #endregion EVENTS
    }

    #region EVENTARGS
    public class MainDataGridEventArgs : EventArgs
    {
        private object dbDataSet;

        public MainDataGridEventArgs(object dbDS)
        {
            dbDataSet = dbDS;
        }

        public object DBDataSet
        {
            get
            {
                return (dbDataSet);
            }
        }
    }
    #endregion EVENTARGS


}
